﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;
using LLKanWebService.Model;
using MySql.Data.Types;
using System.Reflection;
namespace LLKanWebService.BLL
{
    public class UserManager : MySQLHepler
    {

        public bool registerUser(Users user)
        {
            ScoreManager scm = new ScoreManager();
            if (isRegister(user.Account))
            {
                return false;
            }
            openConn();
            MySqlCommand cmd = new MySqlCommand("insert into user(account,password,nickname,register_time) values(?account,?password,?nickname,now())");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("account", user.Account);
            cmd.Parameters.AddWithValue("password", user.Passwrod);
            cmd.Parameters.AddWithValue("nickname", user.NickName);
            int count = cmd.ExecuteNonQuery();
            closeConn();
            scm.changeScore(findUserByAccount(user.Account).ID, 0);
            if (count > 0)
            {
                return true;
            }
            return false;
        }
        public Users findUserByAccount(String account)
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from user where account = ?account");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("account", account);
            MySqlDataReader reader = cmd.ExecuteReader();
            Users user = new Users();
            if (reader.Read())
            {

                user.ID = reader.GetInt32("id");
                user.Account = reader.GetString("account");
                user.Passwrod = reader.GetString("password");
                user.NickName = reader.GetString("nickname");
                user.Money = reader.GetInt32("money");
                user.Benevolence = reader.GetInt32("benevolence");
                if (!reader.IsDBNull(8))
                {
                    user.BenvolenceTime = DateTime.Parse(reader.GetMySqlDateTime("benevolence_time").ToString());
                }
                user.Experience = reader.GetInt32("experience");
                user.RegisterTime = DateTime.Parse(reader.GetMySqlDateTime("register_time").ToString());
            }
            reader.Close();
            closeConn();
            return user;
        }
        public Users findUserById(int userid)
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from user where id = ?userid");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("userid", userid);
            MySqlDataReader reader = cmd.ExecuteReader();
            Users user = new Users();
            if (reader.Read())
            {

                user.ID = reader.GetInt32("id");
                user.Account = reader.GetString("account");
                user.Passwrod = reader.GetString("password");
                user.NickName = reader.GetString("nickname");
                user.Money = reader.GetInt32("money");
                user.Benevolence = reader.GetInt32("benevolence");
                if (!reader.IsDBNull(8))
                {
                    user.BenvolenceTime = DateTime.Parse(reader.GetMySqlDateTime("benevolence_time").ToString());
                }
                user.Experience = reader.GetInt32("experience");
                user.RegisterTime = DateTime.Parse(reader.GetMySqlDateTime("register_time").ToString());
            }
            reader.Close();
            closeConn();
            return user;
        }
        public List<Users> findAll()
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from user");
            cmd.Connection = connection;
            MySqlDataReader reader = cmd.ExecuteReader();
            var userlist = new List<Users>();
            while (reader.Read())
            {
                Users user = new Users();
                user.ID = reader.GetInt32("id");
                user.Account = reader.GetString("account");
                user.Passwrod = reader.GetString("password");
                user.NickName = reader.GetString("nickname");
                user.Money = reader.GetInt32("money");
                user.Benevolence = reader.GetInt32("benevolence");
                if (!reader.IsDBNull(8))
                {
                    user.BenvolenceTime = DateTime.Parse(reader.GetMySqlDateTime("benevolence_time").ToString());
                }
                user.Experience = reader.GetInt32("experience");
                user.RegisterTime = DateTime.Parse(reader.GetMySqlDateTime("register_time").ToString());
                userlist.Add(user);
            }
            reader.Close();
            closeConn();
            return userlist;
        }
        public List<Users> benevolenceTimeIsNotNull()
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select * from user where benevolence_time is not null");
            cmd.Connection = connection;
            MySqlDataReader reader = cmd.ExecuteReader();
            var userlist = new List<Users>();
            while (reader.Read())
            {
                Users user = new Users();
                user.ID = reader.GetInt32("id");
                user.Account = reader.GetString("account");
                user.Passwrod = reader.GetString("password");
                user.NickName = reader.GetString("nickname");
                user.Money = reader.GetInt32("money");
                user.Benevolence = reader.GetInt32("benevolence");
                user.BenvolenceTime = DateTime.Parse(reader.GetMySqlDateTime("benevolence_time").ToString());
                user.Experience = reader.GetInt32("experience");
                user.RegisterTime = DateTime.Parse(reader.GetMySqlDateTime("register_time").ToString());
                userlist.Add(user);
            }
            reader.Close();
            closeConn();
            return userlist;
        }
        public bool isRegister(string account)
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("select count(*) as count from user where account = ?account");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("account", account);
            MySqlDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                if (reader.GetInt32("count") > 0)
                {
                    closeConn();
                    return true;
                }
            }
            closeConn();
            return false;
        }
        public bool settlement(Users user)
        {
            openConn();
            MySqlCommand cmd = new MySqlCommand("update user set money = ?money,experience = ?experience where id = ?userid");
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("money", user.Money);
            cmd.Parameters.AddWithValue("experience", user.Experience);
            cmd.Parameters.AddWithValue("userid", user.ID);
            int count = cmd.ExecuteNonQuery();
            closeConn();
            if (count > 0)
            {
                return true;
            }
            return false;
        }
        public bool changeBenvolence(List<Users> userlist)
        {
            openConn();
            String strsql = "";
            for (int i = 0; i < userlist.Count; i++)
            {
                strsql += "update user set benevolence = ?benevolence" + i + ",benevolence_time = ?benevolence_time" + i + " where id = ?userid" + i + ";";
            }
            MySqlCommand cmd = new MySqlCommand(strsql);
            cmd.Connection = connection;
            for (int i = 0; i < userlist.Count; i++)
            {
                cmd.Parameters.AddWithValue("benevolence" + i, userlist[i].Benevolence + 1);
                if (userlist[i].Benevolence < 4)
                {

                    cmd.Parameters.AddWithValue("benevolence_time" + i, DateTime.Now);
                }
                else
                {
                    cmd.Parameters.AddWithValue("benevolence_time" + i, DBNull.Value);
                }
                cmd.Parameters.AddWithValue("userid" + i, userlist[i].ID);
            }
            int count = cmd.ExecuteNonQuery();
            closeConn();
            if (count > 0)
            {
                return true;
            }
            return false;
        }
        public bool changeBenvolence(int userid)
        {
            Users user = findUserById(userid);
            openConn();
            string strsql = "";
            if (user.BenvolenceTime != null)
            {
                strsql = "update user set benevolence = benevolence-1 where id = ?userid;";
            }
            else
            {
                strsql = "update user set benevolence = benevolence-1,benevolence_time = now() where id = ?userid;";
            }
            MySqlCommand cmd = new MySqlCommand(strsql);
            cmd.Connection = connection;
            cmd.Parameters.AddWithValue("userid", userid);
            int count = cmd.ExecuteNonQuery();
            closeConn();
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}